package cn.teduymdj.jxc.controller;


import cn.teduymdj.jxc.myAnnotation.LogAnnotation;
import cn.teduymdj.jxc.pojo.dto.PurchaseListAddNewDTO02;
import cn.teduymdj.jxc.pojo.dto.SelectPurchaseListCondition;
import cn.teduymdj.jxc.pojo.dto.SelectReturnCondition;
import cn.teduymdj.jxc.pojo.entity.Log;
import cn.teduymdj.jxc.pojo.vo.PurchaseListCountListItemVO;
import cn.teduymdj.jxc.pojo.vo.PurchaseListGoodsListItemVO;
import cn.teduymdj.jxc.pojo.vo.PurchaseListListItemVO;
import cn.teduymdj.jxc.service.LogService;
import cn.teduymdj.jxc.service.PurchaseListGoodsService;
import cn.teduymdj.jxc.util.CreateNumUtil;
import cn.teduymdj.jxc.web.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 进货商品Controller层
 */
@Slf4j
@RestController
@RequestMapping("/purchaseListGoods")
public class PurchaseListGoodsController {

    @Autowired
    private PurchaseListGoodsService purchaseListGoodsService;

    @Autowired
    private LogService logService;


    /**
     * 保存进货单信息
     *
     * @param purchaseListAddNewDTO 进货单信息实体
     * @param purchaseListGoodsStr  进货商品信息JSON字符串
     * @return
     */
    @LogAnnotation(content = "保存进货单信息", type = Log.INSERT_ACTION)
    @RequestMapping("/save")
    public JsonResult<Void> save(@RequestBody PurchaseListAddNewDTO02 purchaseListAddNewDTO) {


        // 保存进货清单
        purchaseListGoodsService.save(purchaseListAddNewDTO);

        return JsonResult.ok();

    }

    /**
     * 查询进货单
     *
     * @param purchaseNumber 单号
     * @param supplierId     供应商ID
     * @param state          付款状态
     * @param sTime          开始时间
     * @param eTime          结束时间
     * @return
     */
    @RequestMapping("/list")
    public JsonResult<List<PurchaseListListItemVO>> list(@RequestBody SelectPurchaseListCondition selectPurchaseListCondition) {

        List<PurchaseListListItemVO> purchaseListList =
                purchaseListGoodsService.getPurchaselist(selectPurchaseListCondition);

        return JsonResult.ok(purchaseListList);
    }

    /**
     * 查询进货单商品信息
     *
     * @param purchaseListId 进货单ID
     * @return
     */
    @RequestMapping("/goodsList")
    public JsonResult<List<PurchaseListGoodsListItemVO>> goodsList(Long purchaseListId) {

        List<PurchaseListGoodsListItemVO> purchaseListGoodsList =
                purchaseListGoodsService.getPurchaseListGoodsByPurchaseListId(purchaseListId);

        return JsonResult.ok(purchaseListGoodsList);
    }

    /**
     * 删除进货单及商品信息
     *
     * @param purchaseListId 进货单ID
     * @return
     */
    @RequestMapping("/delete")
    public JsonResult<Void> delete(Long purchaseListId) {


        purchaseListGoodsService.delete(purchaseListId);


        return JsonResult.ok();

    }

    /**
     * 修改进货单付款状态
     *
     * @param id 进货单ID
     * @return
     */
    @LogAnnotation(content = "修改进货单付款状态为付款", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/Paid")
    public JsonResult<Void> setPaid(@PathVariable Long id) {
        log.debug("开始处理【启用管理员】的请求，参数：{}", id);
        purchaseListGoodsService.setPaid(id);
        return JsonResult.ok();
    }

    /**
     * 修改进货单付款状态
     *
     * @param id 进货单ID
     * @return
     */
    @LogAnnotation(content = "修改进货单付款状态为未付款", type = Log.UPDATE_ACTION)
    @PostMapping("/{id:[0-9]+}/Unpaid")
    public JsonResult<Void> setUnpaid(@PathVariable Long id) {
        log.debug("开始处理【禁用管理员】的请求，参数：{}", id);
        purchaseListGoodsService.setUnpaid(id);
        return JsonResult.ok();
    }

    /**
     * 进货商品统计
     *
     * @param sTime       开始时间
     * @param eTime       结束时间
     * @param goodsTypeId 商品类别ID
     * @param codeOrName  编号或商品名称
     * @return
     */
    @RequestMapping("/count")
    public JsonResult<List<PurchaseListCountListItemVO>> count(@RequestBody SelectReturnCondition selectReturnCondition) {

        List<PurchaseListCountListItemVO> list = purchaseListGoodsService.listByCondition(selectReturnCondition);
        return JsonResult.ok(list);

    }

    @RequestMapping("/getNum")
    public JsonResult<String> getNum(){
        return JsonResult.ok(CreateNumUtil.PurNum());
    }

}